package com.mineoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mineoj.common.BaseResponse;
import com.mineoj.common.DeleteRequest;
import com.mineoj.common.ErrorCode;
import com.mineoj.common.ResultUtils;
import com.mineoj.exception.BusinessException;
import com.mineoj.exception.ThrowUtils;
import com.mineoj.model.dto.newscomment.NewsCommentAddRequest;
import com.mineoj.model.dto.newscomment.NewsCommentQueryRequest;
import com.mineoj.model.entity.NewsComment;
import com.mineoj.model.entity.User;
import com.mineoj.model.vo.NewsCommentVO;
import com.mineoj.service.NewsCommentService;
import com.mineoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目提交接口
 *
 
 */
@RestController
@RequestMapping("/news_comment")
@Slf4j
public class NewsCommentController {

    @Resource
    private NewsCommentService newsCommentService;

    @Resource
    private UserService userService;

    /**
     * 提交评论
     *
     * @param newsCommentAddRequest
     * @param request
     * @return 提交记录的 id
     */
    @PostMapping("/add")
    public BaseResponse<Long> doNewsComment(@RequestBody NewsCommentAddRequest newsCommentAddRequest,
            HttpServletRequest request) {
        if (newsCommentAddRequest == null || newsCommentAddRequest.getNewsId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交评论
        final User loginUser = userService.getLoginUser(request);
        long newsCommentId = newsCommentService.doNewsComment(newsCommentAddRequest, loginUser);
        return ResultUtils.success(newsCommentId);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        NewsComment newsComment = newsCommentService.getById(id);
        ThrowUtils.throwIf(newsComment == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!newsComment.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = newsCommentService.deleteNewsComment(id, user);
        return ResultUtils.success(b);
    }


    /**
     * 分页获取评论列表
     *
     * @param newsCommentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<NewsCommentVO>> listNewsCommentByPage(@RequestBody NewsCommentQueryRequest newsCommentQueryRequest,
                                                                         HttpServletRequest request) {
        long current = newsCommentQueryRequest.getCurrent();
        long size = newsCommentQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<NewsComment> newsCommentPage = newsCommentService.page(new Page<>(current, size));
        return ResultUtils.success(newsCommentService.getNewsCommentVOPage(newsCommentPage, request));
    }
}
